package day16.homework13;

public class Test6 {

    public static boolean areEqual(Object obj1, Object obj2) {

        // 1. 如果两个对象都是 null，认为相等
        if (obj1 == null && obj2 == null) {
            return true;
        }

        // 2. 如果其中一个为 null，另一个不为 null，则不相等
        if (obj1 == null || obj2 == null) {
            return false;
        }

        // 3. 使用 instanceof 判断基本数据类型的包装类
        if (obj1 instanceof Integer && obj2 instanceof Integer) {
            return (Integer) obj1 == (Integer) obj2;
        } else if (obj1 instanceof Double && obj2 instanceof Double) {
            return (Double) obj1 == (Double) obj2; // 注意：浮点数比较通常用 epsilon，此处按题目要求判断“值”是否相等
        } else if (obj1 instanceof Boolean && obj2 instanceof Boolean) {
            return (Boolean) obj1 == (Boolean) obj2;
        } else if (obj1 instanceof Long && obj2 instanceof Long) {
            return (Long) obj1 == (Long) obj2;
        } else if (obj1 instanceof Float && obj2 instanceof Float) {
            return (Float) obj1 == (Float) obj2;
        } else if (obj1 instanceof Character && obj2 instanceof Character) {
            return (Character) obj1 == (Character) obj2;
        } else if (obj1 instanceof Byte && obj2 instanceof Byte) {
            return (Byte) obj1 == (Byte) obj2;
        } else if (obj1 instanceof Short && obj2 instanceof Short) {
            return (Short) obj1 == (Short) obj2;
        }
        
        // 4. 引用类型：使用 equals 方法（如 String、自定义类等）
        else {
            return obj1.equals(obj2);
        }
    }

    // 测试方法
    public static void main(String[] args) {
        // 测试基本数据类型（自动装箱）
        System.out.println(areEqual(1, 1));           // true
        System.out.println(areEqual(1.5, 1.5));       // true
        System.out.println(areEqual(true, true));       // true
        System.out.println(areEqual('A', 'A'));         // true

        // 测试引用类型
        System.out.println(areEqual("Hello", "Hello")); // true
        System.out.println(areEqual(new String("Test"), new String("Test"))); // true

        // 测试不相等
        System.out.println(areEqual(1, 2));             // false
        System.out.println(areEqual("abc", "def"));     // false

        // 测试 null
        System.out.println(areEqual(null, null));       // true
        System.out.println(areEqual("abc", null));      // false
    }
}